##### R CODE TO REPLICATE THE FIGURES AND TABLES INCLUDED IN 
##### Guillermo Rosas and Adrian Lucardi, "Jumping Ship or Jumping on the Bandwagon: When Do Local Politicians Support National Candidates?," Political Science Research and Methods, 8(1), 2020, 60-74.


########## Warning: some of the required packages will not run in R version 3.5.x. Please run this script in R version 3.4.x ##########


## saving the output to a log file
sink (file="Log Replication Jumping Ship or Jumping on the Bandwagon PSRM.txt", append=FALSE, type="output", split=FALSE)

## Loading the required R packages. Make sure to have them installed before proceeding. In order to install a package called X, write install.packages("X")
library (car)
library (ggplot2)
library (lfe)
library (lmtest)
library (MASS)
library (matrixStats)
library (multiwayvcov)
library (plyr)
library (rdrobust)
library (xtable)

## function for adding parentheses automatically -> for the tables
parSE <- function (x, n.digits=2){
  txt <- paste ("%.", n.digits, "f", sep="")
  x.tmp <- matrix (as.character (sprintf (as.character (txt), as.matrix (round (x, n.digits)))), ncol=ncol (x), byrow=F)
  
  # calculating the p-values
  pvals <- matrix (
    (1 - pnorm (unlist (abs (x[seq (1, nrow (x), by=2),] / x[seq (2, nrow (x), by=2),]))))*2
    , nrow=nrow (x)/2, ncol=ncol (x))
  pvals[is.na (pvals)] <- 100 ## because NA's are problematic
  
  # adding the parentheses and stars
  x.tmp[seq (2, nrow (x.tmp), by=2),] <- paste ("(", x.tmp[seq (2, nrow (x.tmp), by=2),], ")", sep="")
  x.tmp[seq (2, nrow (x.tmp), by=2),][pvals<=0.05] <- paste (x.tmp[seq (2, nrow (x.tmp), by=2),][pvals<=0.05], "$^{*}$", sep="")
  x.tmp[seq (2, nrow (x.tmp), by=2),][pvals>0.05 & pvals<=0.10] <- paste (x.tmp[seq (2, nrow (x.tmp), by=2),][pvals>0.05 & pvals<=0.10], "$^{\\dagger}$", sep="")
  x.tmp <- sub ("\\(NA\\)", x.tmp, replacement="")
  x.tmp <- sub ("NA", x.tmp, replacement="")
  return (x.tmp)
}

## display options
options (digits=4, scipen=8, show.signif.stars=FALSE)



#%%%%%%%%%%%%%%%%%%%%%%%%%%%%#
#### Downloading the data ####
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%#

## district-level data; governor party
base.dist.g <- read.csv ("Base Districts 2000-2012 - Governor party.csv", header=T, sep=",")

# re-factorizing and updating the contrasts
base.dist.g$districtYear <- factor (base.dist.g$districtYear)
base.dist.g$state <- factor (base.dist.g$state)
base.dist.g$incGober <- factor (base.dist.g$incGober)
base.dist.g$runnerUp <- factor (base.dist.g$runnerUp)
base.dist.g$stateYear <- factor (with (base.dist.g, paste (state, year, sep="_")))
base.dist.g$year.zero <- factor (base.dist.g$year)
contrasts (base.dist.g$year.zero) <- "contr.sum"

## district-level data; runner-up party
base.dist.r <- read.csv ("Base Districts 2000-2012 - Opposition party.csv", header=T, sep=",")

# re-factorizing and updating the contrasts
base.dist.r$districtYear <- factor (base.dist.r$districtYear)
base.dist.r$state <- factor (base.dist.r$state)
base.dist.r$incGober <- factor (base.dist.r$incGober)
base.dist.r$runnerUp <- factor (base.dist.r$runnerUp)
base.dist.r$stateYear <- factor (with (base.dist.r, paste (state, year, sep="_")))
base.dist.r$year.zero <- factor (base.dist.r$year)
contrasts (base.dist.r$year.zero) <- "contr.sum"

## municipality-level data; all parties
base.party <- read.csv ("Base Municipalities 2000-2012 - All parties.csv", header=T, sep=",")

# re-factorizing
base.party$stateYear <- factor (base.party$stateYear)
base.party$districtYear <- factor (base.party$districtYear)
base.party$muniDistrictYear.lag <- factor (base.party$muniDistrictYear.lag)
base.party$muniDistrictYear <- factor (base.party$muniDistrictYear)
base.party$state <- factor (base.party$state)
base.party$muni <- factor (base.party$muni)
base.party$district <- factor (base.party$district)
base.party$incGober <- factor (base.party$incGober)
base.party$runnerUp <- factor (base.party$runnerUp)

## municipality-level data; split by governor vs. opposition party
base <- read.csv ("Base Municipalities 2000-2012 - Governor and Opposition.csv", header=T, sep=",")

# re-factorizing
base$electionMuni <- factor (base$electionMuni)
base$sample <- factor (base$sample)
base$muniDistrictYear <- factor (base$muniDistrictYear)
base$state <- factor (base$state)
base$district <- factor (base$district)
base$muni <- factor (base$muni)
base$incGober <- factor (base$incGober)
base$runnerUp <- factor (base$runnerUp)




#%%%%%%%%%%%%%%%%%%%%%%%%%%#
#### Doing the analysis ####
#%%%%%%%%%%%%%%%%%%%%%%%%%%#

### Replicating the models reported in Table 2: winDistrict as outcome

## Panel A: Full sample

# governor's party
(sum.gov.win02 <- summary (gov.win02 <- felm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.g)))
(sum.gov.win03 <- summary (gov.win03 <- felm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.g)))  ## warnings reported; don't worry, the results are OK

# runner-up's party
(sum.rup.win02 <- summary (rup.win02 <- felm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.r)))
(sum.rup.win03 <- summary (rup.win03 <- felm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.r)))


## Panel B: PRI only

# governor's party
(sum.gov.win02pri <- summary (gov.win02pri <- felm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))
(sum.gov.win03pri <- summary (gov.win03pri <- felm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))

# runner-up's party
(sum.rup.win02pri <- summary (rup.win02pri <- felm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))
(sum.rup.win03pri <- summary (rup.win03pri <- felm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))




### Replicating the models reported in Table 3 (RD of incumbency on turnout, PRI only)

# looking at sample sizes -> reported in fn. 28 in the text
with (base.party, xtabs (~ incPRI.gober + propPRI.large))
with (base.party, xtabs (~ incPAN.gober + propPAN.large))
with (base.party, xtabs (~ incPRD.gober + propPRD.large))

# Copartisan governor, many copartisan mayors
summary (rdPRI.tnt02 <- with (base.party[base.party$incPRI.gober==1 & base.party$propPRI.large==1,], rdrobust (y=fed.tnt, x=PRI.muni.mg, fuzzy=NULL, covs=NULL, level=95, p=1, q=2, all=T)))
rdPRI.tnt02$coef; rdPRI.tnt02$se

# Copartisan governor, few copartisan mayors
summary (rdPRI.tnt03 <- with (base.party[base.party$incPRI.gober==1 & base.party$propPRI.large==0,], rdrobust (y=fed.tnt, x=PRI.muni.mg, fuzzy=NULL, covs=NULL, level=95, p=1, q=2, all=T)))
rdPRI.tnt03$coef; rdPRI.tnt03$se

# Non-copartisan governor, many copartisan mayors
summary (rdPRI.tnt04 <- with (base.party[base.party$incPRI.gober==0 & base.party$propPRI.large==1,], rdrobust (y=fed.tnt, x=PRI.muni.mg, fuzzy=NULL, covs=NULL, level=95, p=1, q=2, all=T)))
rdPRI.tnt04$coef; rdPRI.tnt04$se

# Non-copartisan governor, few copartisan mayors
summary (rdPRI.tnt05 <- with (base.party[base.party$incPRI.gober==0 & base.party$propPRI.large==0,], rdrobust (y=fed.tnt, x=PRI.muni.mg, fuzzy=NULL, covs=NULL, level=95, p=1, q=2, all=T)))
rdPRI.tnt05$coef; rdPRI.tnt05$se



### Replicating the models reported in Table A4: 2SLS with winDistrict as outcome (PRI only). Instrument is population-weighted proportion of municipal elections decided by a margin of 5 percentage points or less

## Panel A: Second stage

# governor's party
(sum.gov.win22pria <- summary (gov.win22pria <- felm (winDistrict ~ gover.close050.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | (propIncMuni ~ gover.treat050.w) | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))
(sum.gov.win23pria <- summary (gov.win23pria <- felm (winDistrict ~ gover.close050.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | (propIncMuni ~ gover.treat050.w) | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))

# runner-up's party
(sum.rup.win22pria <- summary (rup.win22pria <- felm (winDistrict ~ runnerup.close050.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | (propIncMuni ~ runnerup.treat050.w) | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))
(sum.rup.win23pria <- summary (rup.win23pria <- felm (winDistrict ~ runnerup.close050.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | (propIncMuni ~ runnerup.treat050.w) | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))


## Panel B: First stage

# governor's party
(sum.gov.win22prib <- summary (gov.win22prib <- felm (propIncMuni ~ gover.treat050.w + gover.close050.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))
(sum.gov.win23prib <- summary (gov.win23prib <- felm (propIncMuni ~ gover.treat050.w + gover.close050.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))

# runner-up's party
(sum.rup.win22prib <- summary (rup.win22prib <- felm (propIncMuni ~ runnerup.treat050.w + runnerup.close050.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))
(sum.rup.win23prib <- summary (rup.win23prib <- felm (propIncMuni ~ runnerup.treat050.w + runnerup.close050.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))




### Replicating the models reported in Table A5: 2SLS with winDistrict as outcome (PRI only). Instrument is the (unweighted) proportion of municipal elections decided by a margin of 5 percentage points or less

## Panel A: Second stage

# governor's party
(sum.gov.win32pria <- summary (gov.win32pria <- felm (winDistrict ~ gover.close050 + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | (propIncMuni ~ gover.treat050) | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))
(sum.gov.win33pria <- summary (gov.win33pria <- felm (winDistrict ~ gover.close050 + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | (propIncMuni ~ gover.treat050) | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))

# runner-up's party
(sum.rup.win32pria <- summary (rup.win32pria <- felm (winDistrict ~ runnerup.close050 + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | (propIncMuni ~ runnerup.treat050) | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))
(sum.rup.win33pria <- summary (rup.win33pria <- felm (winDistrict ~ runnerup.close050 + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | (propIncMuni ~ runnerup.treat050) | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))


## Panel B: First stage

# governor's party
(sum.gov.win32prib <- summary (gov.win32prib <- felm (propIncMuni ~ gover.treat050 + gover.close050 + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))
(sum.gov.win33prib <- summary (gov.win33prib <- felm (propIncMuni ~ gover.treat050 + gover.close050 + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))

# runner-up's party
(sum.rup.win32prib <- summary (rup.win32prib <- felm (propIncMuni ~ runnerup.treat050 + runnerup.close050 + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))
(sum.rup.win33prib <- summary (rup.win33prib <- felm (propIncMuni ~ runnerup.treat050 + runnerup.close050 + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))




### Replicating the models reported in Table A6: 2SLS with winDistrict as outcome (PRI only). Instrument is population-weighted proportion of municipal elections decided by a margin of 2.5 percentage points or less

## Panel A: Second stage

# governor's party
(sum.gov.win42pria <- summary (gov.win42pria <- felm (winDistrict ~ gover.close025.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | (propIncMuni ~ gover.treat025.w) | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))
(sum.gov.win43pria <- summary (gov.win43pria <- felm (winDistrict ~ gover.close025.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | (propIncMuni ~ gover.treat025.w) | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))

# runner-up's party
(sum.rup.win42pria <- summary (rup.win42pria <- felm (winDistrict ~ runnerup.close025.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | (propIncMuni ~ runnerup.treat025.w) | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))
(sum.rup.win43pria <- summary (rup.win43pria <- felm (winDistrict ~ runnerup.close025.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | (propIncMuni ~ runnerup.treat025.w) | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))


## Panel B: First stage

# governor's party
(sum.gov.win42prib <- summary (gov.win42prib <- felm (propIncMuni ~ gover.treat025.w + gover.close025.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))
(sum.gov.win43prib <- summary (gov.win43prib <- felm (propIncMuni ~ gover.treat025.w + gover.close025.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))

# runner-up's party
(sum.rup.win42prib <- summary (rup.win42prib <- felm (propIncMuni ~ runnerup.treat025.w + runnerup.close025.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))
(sum.rup.win43prib <- summary (rup.win43prib <- felm (propIncMuni ~ runnerup.treat025.w + runnerup.close025.w + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))




### Replicating the models reported in Table A7: winDistrict as outcome (probit)

## Panel A: Full sample

# governor's party
sum.gov.win12 <- summary (gov.win12 <- glm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero, data=base.dist.g, family=binomial (link="probit")))
vcov.gov.win12 <- cluster.vcov (gov.win12, cluster = ~ state)
(coef.gov.win12 <- coeftest (gov.win12, vcov.gov.win12))
sum.gov.win13 <- summary (gov.win13 <- glm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state), data=base.dist.g, family=binomial (link="probit")))
vcov.gov.win13 <- cluster.vcov (gov.win13, cluster = ~ state)
(coef.gov.win13 <- coeftest (gov.win13, vcov.gov.win13))

# runner-up's party
sum.rup.win12 <- summary (rup.win12 <- glm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero, data=base.dist.r, family=binomial (link="probit")))
vcov.rup.win12 <- cluster.vcov (rup.win12, cluster = ~ state)
(coef.rup.win12 <- coeftest (rup.win12, vcov.rup.win12))
sum.rup.win13 <- summary (rup.win13 <- glm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state), data=base.dist.r, family=binomial (link="probit")))
vcov.rup.win13 <- cluster.vcov (rup.win13, cluster = ~ state)
(coef.rup.win13 <- coeftest (rup.win13, vcov.rup.win13))


## Panel B: PRI only

# governor's party
sum.gov.win12pri <- summary (gov.win12pri <- glm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero, data=base.dist.g[base.dist.g$incGober=="PRI",], family=binomial (link="probit")))
vcov.gov.win12pri <- cluster.vcov (gov.win12pri, cluster = ~ state)
(coef.gov.win12pri <- coeftest (gov.win12pri, vcov.gov.win12))
sum.gov.win13pri <- summary (gov.win13pri <- glm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state), data=base.dist.g[base.dist.g$incGober=="PRI",], family=binomial (link="probit")))
vcov.gov.win13pri <- cluster.vcov (gov.win13pri, cluster = ~ state)
(coef.gov.win13pri <- coeftest (gov.win13pri, vcov.gov.win13))

# runner-up's party
sum.rup.win12pri <- summary (rup.win12pri <- glm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero, data=base.dist.r[base.dist.r$runnerUp=="PRI",], family=binomial (link="probit")))
vcov.rup.win12pri <- cluster.vcov (rup.win12pri, cluster = ~ state)
(coef.rup.win12pri <- coeftest (rup.win12pri, vcov.rup.win12))
sum.rup.win13pri <- summary (rup.win13pri <- glm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state), data=base.dist.r[base.dist.r$runnerUp=="PRI",], family=binomial (link="probit")))
vcov.rup.win13pri <- cluster.vcov (rup.win13pri, cluster = ~ state)
(coef.rup.win13pri <- coeftest (rup.win13pri, vcov.rup.win13))




### Replicating the models reported in Table A8: Vote Share as outcome

## Panel A: Full sample

# governor's party
(sum.gov.sh02 <- summary (gov.sh02 <- felm (dist.sh ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.g)))
(sum.gov.sh03 <- summary (gov.sh03 <- felm (dist.sh ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.g)))

# runner-up's party
(sum.rup.sh02 <- summary (rup.sh02 <- felm (dist.sh ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.r)))
(sum.rup.sh03 <- summary (rup.sh03 <- felm (dist.sh ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.r)))


## Panel B: PRI only

# governor's party
(sum.gov.sh02pri <- summary (gov.sh02pri <- felm (dist.sh ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))
(sum.gov.sh03pri <- summary (gov.sh03pri <- felm (dist.sh ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))

# runner-up's party
(sum.rup.sh02pri <- summary (rup.sh02pri <- felm (dist.sh ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))
(sum.rup.sh03pri <- summary (rup.sh03pri <- felm (dist.sh ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))




### Replicating the models reported in Table A9: Margin of victory as outcome

## Panel A: Full sample

# governor's party
(sum.gov.mg02 <- summary (gov.mg02 <- felm (dist.mg ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.g)))
(sum.gov.mg03 <- summary (gov.mg03 <- felm (dist.mg ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.g)))

# runner-up's party
(sum.rup.mg02 <- summary (rup.mg02 <- felm (dist.mg ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.r)))
(sum.rup.mg03 <- summary (rup.mg03 <- felm (dist.mg ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.r)))


## Panel B: PRI only

# governor's party
(sum.gov.mg02pri <- summary (gov.mg02pri <- felm (dist.mg ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))
(sum.gov.mg03pri <- summary (gov.mg03pri <- felm (dist.mg ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))

# runner-up's party
(sum.rup.mg02pri <- summary (rup.mg02pri <- felm (dist.mg ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))
(sum.rup.mg03pri <- summary (rup.mg03pri <- felm (dist.mg ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))




### Replicating the models reported in Table A10: controlling for the effective number of municipalities

## Panel A: Full sample

# governor's party
(sum.gov.win05 <- summary (gov.win05 <- felm (winDistrict ~ propIncMuni + n.munisEff + dist.mg.lag + poverty + n.munisEff + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.g)))
(sum.gov.win06 <- summary (gov.win06 <- felm (winDistrict ~ propIncMuni + n.munisEff + dist.mg.lag + poverty + n.munisEff + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.g)))

# runner-up's party
(sum.rup.win05 <- summary (rup.win05 <- felm (winDistrict ~ propIncMuni + n.munisEff + dist.mg.lag + poverty + n.munisEff + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.r)))
(sum.rup.win06 <- summary (rup.win06 <- felm (winDistrict ~ propIncMuni + n.munisEff + dist.mg.lag + poverty + n.munisEff + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.r)))


## Panel B: PRI only

# governor's party
(sum.gov.win05pri <- summary (gov.win05pri <- felm (winDistrict ~ propIncMuni + n.munisEff + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))
(sum.gov.win06pri <- summary (gov.win06pri <- felm (winDistrict ~ propIncMuni + n.munisEff + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.g[base.dist.g$incGober=="PRI",])))

# runner-up's party
(sum.rup.win05pri <- summary (rup.win05pri <- felm (winDistrict ~ propIncMuni + n.munisEff + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero | 0 | 0 | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))
(sum.rup.win06pri <- summary (rup.win06pri <- felm (winDistrict ~ propIncMuni + n.munisEff + dist.mg.lag + poverty + poly (normal.sh, 4) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",])))




### Replicating the models reported in Table A11 (RD estimates of municipal incumbency on municipal elections)

## DV: Copartisan victory

# governor's party
summary ((gov.win01rdd <- with (base[base$sample=="governor party",], rdrobust (y=winMuni, x=muni.mg, fuzzy=NULL, covs=NULL, level=95, p=1, q=2, all=T))))
gov.win01rdd$coef; gov.win01rdd$se

# runner-up's party
summary ((rup.win01rdd <- with (base[base$sample=="runner-up party",], rdrobust (y=winMuni, x=muni.mg, fuzzy=NULL, covs=NULL, level=95, p=1, q=2, all=T))))
rup.win01rdd$coef; rup.win01rdd$se


## DV: Turnout

# governor's party
summary ((gov.tnt01rdd <- with (base[base$sample=="governor party",], rdrobust (y=tnt.fed, x=muni.mg, fuzzy=NULL, covs=NULL, level=95, p=1, q=2, all=T))))
gov.tnt01rdd$coef; gov.tnt01rdd$se

# runner-up's party
summary ((rup.tnt01rdd <- with (base[base$sample=="runner-up party",], rdrobust (y=tnt.fed, x=muni.mg, fuzzy=NULL, covs=NULL, level=95, p=1, q=2, all=T))))
rup.tnt01rdd$coef; rup.tnt01rdd$se





#%%%%%%%%%%%%%%%%%%%%%%%%%%%#
#### Building the tables ####
#%%%%%%%%%%%%%%%%%%%%%%%%%%%#

## Setting values that we will keep constant when presenting the results
(n.digit <- 2) ## number of decimals to use

## Code for replicating Table 2
(tab.coefs01 <- cbind (
  c (coef(sum.gov.win02)[c(2:4,1),1], coef(sum.gov.win02pri)[c (2:4,1),1])
  , c (coef(sum.gov.win03)[c(2:4,1),1], coef(sum.gov.win03pri)[c (2:4,1),1])
  , rep (NA, 8)
  , c (coef(sum.rup.win02)[c(2:4,1),1], coef(sum.rup.win02pri)[c (2:4,1),1])
  , c (coef(sum.rup.win03)[c(2:4,1),1], coef(sum.rup.win03pri)[c (2:4,1),1])
))
(tab.ses01 <- cbind (
  c (coef(sum.gov.win02)[c(2:4,1),2], coef(sum.gov.win02pri)[c (2:4,1),2])
  , c (coef(sum.gov.win03)[c(2:4,1),2], coef(sum.gov.win03pri)[c (2:4,1),2])
  , rep (NA, 8)
  , c (coef(sum.rup.win02)[c(2:4,1),2], coef(sum.rup.win02pri)[c (2:4,1),2])
  , c (coef(sum.rup.win03)[c(2:4,1),2], coef(sum.rup.win03pri)[c (2:4,1),2])
))

odd <- seq (1, nrow (tab.coefs01)*2, by=2)
even <- seq (2, nrow (tab.coefs01)*2, by=2)

(tab.results01 <- data.frame (rbind (tab.coefs01, tab.ses01)))
rownames (tab.results01) <- c (odd, even)
tab.results01 <- tab.results01[order (as.numeric (rownames (tab.results01))),]

rows <- c ("Proportion of copartisan mayors", ""
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Intercept", ""
           , "Proportion of copartisan mayors", ""
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Intercept", "")

(nobs01 <- c (
  sum.gov.win02$N, sum.gov.win03$N
  , sum.rup.win02$N, sum.rup.win03$N))
(nobs01pri <- c (
  sum.gov.win02pri$N, sum.gov.win03pri$N
  , sum.rup.win02pri$N, sum.rup.win03pri$N))
(sigma01 <- c (
  sum.gov.win02$rse, sum.gov.win03$rse
  , sum.rup.win02$rse, sum.rup.win03$rse))
(sigma01pri <- c (
  sum.gov.win02pri$rse, sum.gov.win03pri$rse
  , sum.rup.win02pri$rse, sum.rup.win03pri$rse))

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\emph{Copartisan governor}} && \\multicolumn{2}{c}{\\emph{Opposition governor}} \\\\ \\cline{2-3} \\cline{5-6} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} && \\multicolumn{1}{c}{(3)} & \\multicolumn{1}{c}{(4)} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{6}{l}{\\underline{(a) Full Sample}} \\\\ [1.5ex] \n")
Header5 <- paste ("\\multicolumn{6}{l}{\\underline{(b) PRI-only Sample}} \\\\ [1.5ex] \n")

add1a <- paste ("\\midrule RMSE & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma01[1],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma01[2],2)), "} && \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma01[3],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma01[4],2)), "}  \\\\ \n")
add1b <- paste ("num. obs. & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs01[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs01[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs01[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs01[4],0)), "}  \\\\ \\midrule \n")
add2a <- paste ("\\midrule RMSE & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma01pri[1],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma01pri[2],2)), "} && \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma01pri[3],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma01pri[4],2)), "}  \\\\ \n")
add2b <- paste ("num. obs. & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs01pri[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs01pri[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs01pri[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs01pri[4],0)), "}  \\\\ \\midrule \n")
add3 <- paste ("Previous vote share & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add4 <- paste ("Year effects & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add5 <- paste ("State effects & \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 8
addtorow$pos[[5]] <- 8
addtorow$pos[[6]] <- 8
addtorow$pos[[7]] <- 16
addtorow$pos[[8]] <- 16
addtorow$pos[[9]] <- 16
addtorow$pos[[10]] <- 16
addtorow$pos[[11]] <- 16
addtorow$command <- c (Header1, Header3, Header4
                       , add1a, add1b, Header5
                       , add2a, add2b, add3, add4, add5)
print (xtable (  cbind (rows, parSE (tab.results01, n.digits=n.digit))
                 , align=c("l","l","c","c","c","c","c")
                 , digits=n.digit
                 , caption="LPM estimates of \\emph{proportion of copartisan mayors} on a congressional candidate's \\emph{probability of victory} in Mexico, 2000-2012"
                 , label="T:mainResults")
       , sanitize.rownames.function=function(x){x}
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="small"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow  )



## Code for replicating Figure 2

set.seed (240542442)  ## for reproducibility
m <- 200  ## unique values of proportion copartisan mayors
sims <- 999

dEffects <- as.data.frame (factor (c (rep ("(a) Full Sample", m*2), rep ("(b) PRI-only Sample", m*2))))
colnames (dEffects)[1] <- "sample"
dEffects$subsample <- factor (c (rep ("(b) governor", m), rep ("(a) runner-up", m), rep ("(b) governor", m), rep ("(a) runner-up", m)))
dEffects$propIncMuni <- rep (seq (0, 1, length.out = m), 4)
dEffects$high975 <- dEffects$high950 <- dEffects$median <- dEffects$low050 <- dEffects$low025 <- NA  ## we'll fill these later


## re-running the models of interest with raw polynomials -> we need this to introduce normal.sh into the equation
sum.gov.win03bpri <- summary (gov.win03bpri <- felm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4, raw=T) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.g[base.dist.g$incGober=="PRI",])) ## warnings reported; don't worry, the results don't change
sum.rup.win03bpri <- summary (rup.win03bpri <- felm (winDistrict ~ propIncMuni + dist.mg.lag + poverty + poly (normal.sh, 4, raw=T) + year.zero + factor (state) | 0 | 0 | state, data=base.dist.r[base.dist.r$runnerUp=="PRI",]))


## getting the distribution of simulated values

# PRI sample, governor's party
cf.gov.win03bpri <- matrix (rep (c (1, NA, mean (base.dist.g[!is.na (base.dist.g$propIncMuni) & base.dist.g$incGober=="PRI",]$dist.mg.lag), mean (base.dist.g[!is.na (base.dist.g$propIncMuni) & base.dist.g$incGober=="PRI",]$poverty), as.numeric (poly (mean (base.dist.g[!is.na (base.dist.g$propIncMuni) & base.dist.g$incGober=="PRI",]$normal.sh), 4, raw=T)), rep (0, 20)), m), nrow=m, byrow=T)

cf.gov.win03bpri[,2] <- seq (0, 1, length.out = m) ## propIncMuni from 0 to 1

dEffects[dEffects$sample=="(b) PRI-only Sample" & dEffects$subsample=="(b) governor",c ("low025", "low050", "median", "high950", "high975")] <- colQuantiles (mvrnorm (sims, mu=sum.gov.win03bpri$coef[,1], Sigma=vcov (gov.win03bpri)) %*% t (cf.gov.win03bpri)
                                                                                                                                           , probs = c (0.025, 0.05, 0.50, 0.95, 0.975))

# PRI sample, runner-up's party
cf.rup.win03bpri <- matrix (rep (c (1, NA, mean (base.dist.r[!is.na (base.dist.r$propIncMuni) & base.dist.r$runnerUp=="PRI",]$dist.mg.lag), mean (base.dist.r[!is.na (base.dist.r$propIncMuni) & base.dist.r$runnerUp=="PRI",]$poverty), as.numeric (poly (mean (base.dist.r[!is.na (base.dist.r$propIncMuni) & base.dist.r$runnerUp=="PRI",]$normal.sh), 4, raw=T)), rep (0, 14)), m), nrow=m, byrow=T)

cf.rup.win03bpri[,2] <- seq (0, 1, length.out = m) ## propIncMuni from 0 to 1

dEffects[dEffects$sample=="(b) PRI-only Sample" & dEffects$subsample=="(a) runner-up",c ("low025", "low050", "median", "high950", "high975")] <- colQuantiles (mvrnorm (sims, mu=sum.rup.win03bpri$coef[,1], Sigma=vcov (rup.win03bpri)) %*% t (cf.rup.win03bpri)
                                                                                                                                            , probs = c (0.025, 0.05, 0.50, 0.95, 0.975))

# setting the graphical parameters
cex.text <- 18
plotWidth <- 840
aspect.ratio <- 2.1

# drawing the plot
(pEffectsPRI <- ggplot (dEffects[dEffects$sample=="(b) PRI-only Sample",]
                     , aes (x=propIncMuni, y=median, fill=subsample, col=subsample))
  + geom_ribbon (aes (ymin=low025, ymax=high975), alpha=0.4)
  + geom_line (size=cex.text/18)
  + theme_bw ()
  + theme (text=element_text (size=cex.text))
  + theme (strip.text.x = element_text (size = cex.text))
  + ylim (-0.05, 0.85)
  + xlab ("proportion of copartisan mayors")
  + ylab ("Pr (winning the district)")
  + theme (legend.title = element_blank ())
  + scale_color_grey (labels=c ("PRI governor", "opposition governor"))
  + scale_fill_grey (labels=c ("PRI governor", "opposition governor"))
  )

# exporting the plot
png ("fig_MainResultsPRI.png", w=plotWidth/1.10, h=plotWidth/(aspect.ratio*1.25))
pEffectsPRI
dev.off ()


## Code for replicating Table 3

(tab.coefs02 <- rbind (
  c (rdPRI.tnt03$coef[2], rdPRI.tnt05$coef[2])
  , c (rdPRI.tnt02$coef[2], rdPRI.tnt04$coef[2])
  ))
(tab.ses02 <- rbind (
  c (rdPRI.tnt03$se[2], rdPRI.tnt05$se[2])
  , c (rdPRI.tnt02$se[2], rdPRI.tnt04$se[2])
  ))
(tab.ns02 <- rbind (
  c (rdPRI.tnt03$N_h_l+rdPRI.tnt03$N_h_r, rdPRI.tnt05$N_h_l+rdPRI.tnt05$N_h_r)
  , c (rdPRI.tnt02$N_h_l+rdPRI.tnt02$N_h_r, rdPRI.tnt04$N_h_l+rdPRI.tnt04$N_h_r)
  ))

odd <- seq (1, nrow (tab.coefs02)*2, by=2)
even <- seq (2, nrow (tab.coefs02)*2, by=2)

(tab.results02 <- data.frame (rbind (tab.coefs02, tab.ses02)))
rownames (tab.results02) <- c (odd, even)
tab.results02 <- tab.results02[order (as.numeric (rownames (tab.results02))),]

rows <- c ("", "\\multicolumn{1}{c}{\\# Copartisan mayors}"
           , "\\multicolumn{1}{c}{in district}", "")
rows2 <- c ("\\multicolumn{1}{c}{Few}", ""
           , "\\multicolumn{1}{c}{Many}", "")

Header1 <- paste ("\\toprule & & \\multicolumn{2}{c}{Governor} \\\\ \\cmidrule{3-4} \n")
Header2 <- paste (" & & \\multicolumn{1}{c}{Copartisan} & \\multicolumn{1}{c}{Opposition} \\\\ \\midrule \n")
add1 <- paste ("\\midrule \\multicolumn{2}{l}{$p$-value for test of equal means} & \\multicolumn{1}{c}{", sprintf ("%.3f", round ((1 - pnorm (abs ((rdPRI.tnt03$coef[2] - rdPRI.tnt02$coef[2]) / sqrt (rdPRI.tnt03$se[2]^2 + rdPRI.tnt02$se[2]^2))))*2,3)), "} & \\multicolumn{1}{c}{", sprintf ("%.3f", round ((1 - pnorm (abs ((rdPRI.tnt05$coef[2] - rdPRI.tnt04$coef[2]) / sqrt (rdPRI.tnt05$se[2]^2 + rdPRI.tnt04$se[2]^2))))*2,3)), "}  \\\\ \\bottomrule \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 4
addtorow$command <- c (Header1, Header2, add1)
print (xtable (  cbind (rows, rows2, parSE (tab.results02, n.digits=n.digit+1))
                 , align=c("l","l","c","c","c")
                 , digits=n.digit+1
                 , caption="RD estimates of the effect of PRI incumbency at the municipal level on \\emph{turnout} in federal elections (measured at the municipal level), 2000-2012"
                 , label="T:partialSubstituteExplanation")
       , sanitize.rownames.function=function(x){x}
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="small"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow  )



## Code for replicating Table A4
(tab.coefs03 <- cbind (
  c (coef (sum.gov.win22pria)[length (coef (sum.gov.win22pria)[,1]),1], coef(sum.gov.win22pria)[c (3:4,2,1),1], coef (sum.gov.win22prib)[c (2,4:5,3,1),1])
  , c (coef (sum.gov.win23pria)[length (coef (sum.gov.win23pria)[,1]),1], coef(sum.gov.win23pria)[c (3:4,2,1),1], coef (sum.gov.win23prib)[c (2,4:5,3,1),1])
  , rep (NA, 10)
  , c (coef (sum.rup.win22pria)[length (coef (sum.rup.win22pria)[,1]),1], coef(sum.rup.win22pria)[c (3:4,2,1),1], coef (sum.rup.win22prib)[c (2,4:5,3,1),1])
  , c (coef (sum.rup.win23pria)[length (coef (sum.rup.win23pria)[,1]),1], coef(sum.rup.win23pria)[c (3:4,2,1),1], coef (sum.rup.win23prib)[c (2,4:5,3,1),1])
))
(tab.ses03 <- cbind (
  c (coef (sum.gov.win22pria)[length (coef (sum.gov.win22pria)[,2]),2], coef(sum.gov.win22pria)[c (3:4,2,1),2], coef (sum.gov.win22prib)[c (2,4:5,3,1),2])
  , c (coef (sum.gov.win23pria)[length (coef (sum.gov.win23pria)[,2]),2], coef(sum.gov.win23pria)[c (3:4,2,1),2], coef (sum.gov.win23prib)[c (2,4:5,3,1),2])
  , rep (NA, 10)
  , c (coef (sum.rup.win22pria)[length (coef (sum.rup.win22pria)[,2]),2], coef(sum.rup.win22pria)[c (3:4,2,1),2], coef (sum.rup.win22prib)[c (2,4:5,3,1),2])
  , c (coef (sum.rup.win23pria)[length (coef (sum.rup.win23pria)[,2]),2], coef(sum.rup.win23pria)[c (3:4,2,1),2], coef (sum.rup.win23prib)[c (2,4:5,3,1),2])
))

odd <- seq (1, nrow (tab.coefs03)*2, by=2)
even <- seq (2, nrow (tab.coefs03)*2, by=2)

(tab.results03 <- data.frame (rbind (tab.coefs03, tab.ses03)))
rownames (tab.results03) <- c (odd, even)
tab.results03 <- tab.results03[order (as.numeric (rownames (tab.results03))),]

rows <- c ("Proportion of copartisan mayors", "~~~~~(instrumented)"
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Proportion of close elections", ""
           , "Intercept", ""
           , "Proportion barely won/lost", ""
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Proportion of close elections", ""
           , "Intercept", "")

(sigma03a <- c (
  sum.gov.win22pria$rse, sum.gov.win23pria$rse
  , sum.rup.win22pria$rse, sum.rup.win23pria$rse))
(sigma03b <- c (
  sum.gov.win22prib$rse, sum.gov.win23prib$rse
  , sum.rup.win22prib$rse, sum.rup.win23prib$rse))
(fstat03 <- c (
  lfe::waldtest(gov.win22prib, "gover.treat050.w", type="cluster")[5], lfe::waldtest(gov.win23prib, "gover.treat050.w", type="cluster")[5]
  , lfe::waldtest(rup.win22prib, "runnerup.treat050.w", type="cluster")[5], lfe::waldtest(rup.win23prib, "runnerup.treat050.w", type="cluster")[5]))
(nobs03 <- c (
  sum.gov.win22pria$N, sum.gov.win23pria$N
  , sum.rup.win22pria$N, sum.rup.win23pria$N))

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\emph{Copartisan governor}} && \\multicolumn{2}{c}{\\emph{Opposition governor}} \\\\ \\cline{2-3} \\cline{5-6} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} && \\multicolumn{1}{c}{(3)} & \\multicolumn{1}{c}{(4)} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{6}{l}{\\underline{(a) Second stage regression} (outcome: \\emph{copartisan victory})} \\\\ [1.5ex] \n")
Header5 <- paste ("\\multicolumn{6}{l}{\\underline{(b) First stage regression} (outcome: \\emph{proportion of copartisan mayors})} \\\\ [1.5ex] \n")

add1a <- paste ("\\midrule RMSE & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03a[1],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03a[2],2)), "} && \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03a[3],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03a[4],2)), "}  \\\\ \\midrule \n")
add2a <- paste ("\\midrule RMSE & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03b[1],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03b[2],2)), "} && \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03b[3],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03b[4],2)), "}  \\\\ \n")
add2b <- paste ("$F$-statistic (excl. instrument) & \\multicolumn{1}{c}{", sprintf ("%.0f", round (fstat03[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (fstat03[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (fstat03[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (fstat03[4],0)), "}  \\\\ \n")
add3 <- paste ("\\midrule Previous vote share & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add4 <- paste ("Year effects & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add5 <- paste ("State effects & \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add6 <- paste ("num. obs. & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs03[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs03[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs03[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs03[4],0)), "}  \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 10
addtorow$pos[[5]] <- 10
addtorow$pos[[6]] <- 20
addtorow$pos[[7]] <- 20
addtorow$pos[[8]] <- 20
addtorow$pos[[9]] <- 20
addtorow$pos[[10]] <- 20
addtorow$pos[[11]] <- 20
addtorow$command <- c (Header1, Header3, Header4
                       , add1a, Header5
                       , add2a, add2b, add3, add4, add5, add6)
print (xtable (  cbind (rows, parSE (tab.results03, n.digits=n.digit))
                 , align=c("l","l","c","c","c","c","c")
                 , digits=n.digit
                 , caption="2SLS estimates: effect of \\emph{proportion of copartisan mayors} on a congressional candidate's \\emph{probability of victory} in Mexico, 2000-2012 (PRI only)"
                 , label="T:ivResultsMainPRI")
       , sanitize.rownames.function=function(x){x}
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="small"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow  )



## Code for replicating Table A5
(tab.coefs05 <- cbind (
  c (coef (sum.gov.win32pria)[length (coef (sum.gov.win32pria)[,1]),1], coef(sum.gov.win32pria)[c (3:4,2,1),1], coef (sum.gov.win32prib)[c (2,4:5,3,1),1])
  , c (coef (sum.gov.win33pria)[length (coef (sum.gov.win33pria)[,1]),1], coef(sum.gov.win33pria)[c (3:4,2,1),1], coef (sum.gov.win33prib)[c (2,4:5,3,1),1])
  , rep (NA, 10)
  , c (coef (sum.rup.win32pria)[length (coef (sum.rup.win32pria)[,1]),1], coef(sum.rup.win32pria)[c (3:4,2,1),1], coef (sum.rup.win32prib)[c (2,4:5,3,1),1])
  , c (coef (sum.rup.win33pria)[length (coef (sum.rup.win33pria)[,1]),1], coef(sum.rup.win33pria)[c (3:4,2,1),1], coef (sum.rup.win33prib)[c (2,4:5,3,1),1])
))
(tab.ses05 <- cbind (
  c (coef (sum.gov.win32pria)[length (coef (sum.gov.win32pria)[,2]),2], coef(sum.gov.win32pria)[c (3:4,2,1),2], coef (sum.gov.win32prib)[c (2,4:5,3,1),2])
  , c (coef (sum.gov.win33pria)[length (coef (sum.gov.win33pria)[,2]),2], coef(sum.gov.win33pria)[c (3:4,2,1),2], coef (sum.gov.win33prib)[c (2,4:5,3,1),2])
  , rep (NA, 10)
  , c (coef (sum.rup.win32pria)[length (coef (sum.rup.win32pria)[,2]),2], coef(sum.rup.win32pria)[c (3:4,2,1),2], coef (sum.rup.win32prib)[c (2,4:5,3,1),2])
  , c (coef (sum.rup.win33pria)[length (coef (sum.rup.win33pria)[,2]),2], coef(sum.rup.win33pria)[c (3:4,2,1),2], coef (sum.rup.win33prib)[c (2,4:5,3,1),2])
))

odd <- seq (1, nrow (tab.coefs05)*2, by=2)
even <- seq (2, nrow (tab.coefs05)*2, by=2)

(tab.results05 <- data.frame (rbind (tab.coefs05, tab.ses05)))
rownames (tab.results05) <- c (odd, even)
tab.results05 <- tab.results05[order (as.numeric (rownames (tab.results05))),]

rows <- c ("Proportion of copartisan mayors", "~~~~~(instrumented)"
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Proportion of close elections", ""
           , "Intercept", ""
           , "Proportion barely won/lost", ""
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Proportion of close elections", ""
           , "Intercept", "")

(sigma05a <- c (
  sum.gov.win32pria$rse, sum.gov.win33pria$rse
  , sum.rup.win32pria$rse, sum.rup.win33pria$rse))
(sigma05b <- c (
  sum.gov.win32prib$rse, sum.gov.win33prib$rse
  , sum.rup.win32prib$rse, sum.rup.win33prib$rse))
(fstat05 <- c (
  lfe::waldtest(gov.win32prib, "gover.treat050", type="cluster")[5], lfe::waldtest(gov.win33prib, "gover.treat050", type="cluster")[5]
  , lfe::waldtest(rup.win32prib, "runnerup.treat050", type="cluster")[5], lfe::waldtest(rup.win33prib, "runnerup.treat050", type="cluster")[5]))
(nobs05 <- c (
  sum.gov.win32pria$N, sum.gov.win33pria$N
  , sum.rup.win32pria$N, sum.rup.win33pria$N))

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\emph{Copartisan governor}} && \\multicolumn{2}{c}{\\emph{Opposition governor}} \\\\ \\cline{2-3} \\cline{5-6} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} && \\multicolumn{1}{c}{(3)} & \\multicolumn{1}{c}{(4)} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{6}{l}{\\underline{(a) Second stage regression} (outcome: \\emph{copartisan victory})} \\\\ [1.5ex] \n")
Header5 <- paste ("\\multicolumn{6}{l}{\\underline{(b) First stage regression} (outcome: \\emph{proportion of copartisan mayors})} \\\\ [1.5ex] \n")

add1a <- paste ("\\midrule RMSE & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma05a[1],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma05a[2],2)), "} && \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma05a[3],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma05a[4],2)), "}  \\\\ \\midrule \n")
add2a <- paste ("\\midrule RMSE & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma05b[1],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma05b[2],2)), "} && \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma05b[3],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma05b[4],2)), "}  \\\\ \n")
add2b <- paste ("$F$-statistic (excl. instrument) & \\multicolumn{1}{c}{", sprintf ("%.0f", round (fstat05[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (fstat05[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (fstat05[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (fstat05[4],0)), "}  \\\\ \n")
add3 <- paste ("\\midrule Previous vote share & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add4 <- paste ("Year effects & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add5 <- paste ("State effects & \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add6 <- paste ("num. obs. & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs05[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs05[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs05[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs05[4],0)), "}  \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 10
addtorow$pos[[5]] <- 10
addtorow$pos[[6]] <- 20
addtorow$pos[[7]] <- 20
addtorow$pos[[8]] <- 20
addtorow$pos[[9]] <- 20
addtorow$pos[[10]] <- 20
addtorow$pos[[11]] <- 20
addtorow$command <- c (Header1, Header3, Header4
                       , add1a, Header5
                       , add2a, add2b, add3, add4, add5, add6)
print (xtable (  cbind (rows, parSE (tab.results05, n.digits=n.digit))
                 , align=c("l","l","c","c","c","c","c")
                 , digits=n.digit
                 , caption="2SLS estimates: effect of \\emph{proportion of copartisan mayors} on a congressional candidate's \\emph{probability of victory} in Mexico, 2000-2012 (unweighted averages, PRI only)"
                 , label="T:ivResultsUWPRI")
       , sanitize.rownames.function=function(x){x}
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="small"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow  )



## Code for replicating Table A6
(tab.coefs07 <- cbind (
  c (coef (sum.gov.win42pria)[length (coef (sum.gov.win42pria)[,1]),1], coef(sum.gov.win42pria)[c (3:4,2,1),1], coef (sum.gov.win42prib)[c (2,4:5,3,1),1])
  , c (coef (sum.gov.win43pria)[length (coef (sum.gov.win43pria)[,1]),1], coef(sum.gov.win43pria)[c (3:4,2,1),1], coef (sum.gov.win43prib)[c (2,4:5,3,1),1])
  , rep (NA, 10)
  , c (coef (sum.rup.win42pria)[length (coef (sum.rup.win42pria)[,1]),1], coef(sum.rup.win42pria)[c (3:4,2,1),1], coef (sum.rup.win42prib)[c (2,4:5,3,1),1])
  , c (coef (sum.rup.win43pria)[length (coef (sum.rup.win43pria)[,1]),1], coef(sum.rup.win43pria)[c (3:4,2,1),1], coef (sum.rup.win43prib)[c (2,4:5,3,1),1])
))
(tab.ses07 <- cbind (
  c (coef (sum.gov.win42pria)[length (coef (sum.gov.win42pria)[,2]),2], coef(sum.gov.win42pria)[c (3:4,2,1),2], coef (sum.gov.win42prib)[c (2,4:5,3,1),2])
  , c (coef (sum.gov.win43pria)[length (coef (sum.gov.win43pria)[,2]),2], coef(sum.gov.win43pria)[c (3:4,2,1),2], coef (sum.gov.win43prib)[c (2,4:5,3,1),2])
  , rep (NA, 10)
  , c (coef (sum.rup.win42pria)[length (coef (sum.rup.win42pria)[,2]),2], coef(sum.rup.win42pria)[c (3:4,2,1),2], coef (sum.rup.win42prib)[c (2,4:5,3,1),2])
  , c (coef (sum.rup.win43pria)[length (coef (sum.rup.win43pria)[,2]),2], coef(sum.rup.win43pria)[c (3:4,2,1),2], coef (sum.rup.win43prib)[c (2,4:5,3,1),2])
))

odd <- seq (1, nrow (tab.coefs07)*2, by=2)
even <- seq (2, nrow (tab.coefs07)*2, by=2)

(tab.results07 <- data.frame (rbind (tab.coefs07, tab.ses07)))
rownames (tab.results07) <- c (odd, even)
tab.results07 <- tab.results07[order (as.numeric (rownames (tab.results07))),]

rows <- c ("Proportion of copartisan mayors", "~~~~~(instrumented)"
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Proportion of close elections", ""
           , "Intercept", ""
           , "Proportion barely won/lost", ""
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Proportion of close elections", ""
           , "Intercept", "")

(sigma07a <- c (
  sum.gov.win42pria$rse, sum.gov.win43pria$rse
  , sum.rup.win42pria$rse, sum.rup.win43pria$rse))
(sigma07b <- c (
  sum.gov.win42prib$rse, sum.gov.win43prib$rse
  , sum.rup.win42prib$rse, sum.rup.win43prib$rse))
(fstat07 <- c (
  lfe::waldtest(gov.win42prib, "gover.treat025.w", type="cluster")[5], lfe::waldtest(gov.win43prib, "gover.treat025.w", type="cluster")[5]
  , lfe::waldtest(rup.win42prib, "runnerup.treat025.w", type="cluster")[5], lfe::waldtest(rup.win43prib, "runnerup.treat025.w", type="cluster")[5]))
(nobs07 <- c (
  sum.gov.win42pria$N, sum.gov.win43pria$N
  , sum.rup.win42pria$N, sum.rup.win43pria$N))

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\emph{Copartisan governor}} && \\multicolumn{2}{c}{\\emph{Opposition governor}} \\\\ \\cline{2-3} \\cline{5-6} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} && \\multicolumn{1}{c}{(3)} & \\multicolumn{1}{c}{(4)} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{6}{l}{\\underline{(a) Second stage regression} (outcome: \\emph{copartisan victory})} \\\\ [1.5ex] \n")
Header5 <- paste ("\\multicolumn{6}{l}{\\underline{(b) First stage regression} (outcome: \\emph{proportion of copartisan mayors})} \\\\ [1.5ex] \n")

add1a <- paste ("\\midrule RMSE & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma07a[1],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma07a[2],2)), "} && \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma07a[3],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma07a[4],2)), "}  \\\\ \\midrule \n")
add2a <- paste ("\\midrule RMSE & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma07b[1],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma07b[2],2)), "} && \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma07b[3],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma07b[4],2)), "}  \\\\ \n")
add2b <- paste ("$F$-statistic (excl. instrument) & \\multicolumn{1}{c}{", sprintf ("%.0f", round (fstat07[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (fstat07[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (fstat07[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (fstat07[4],0)), "}  \\\\ \n")
add3 <- paste ("\\midrule Previous vote share & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add4 <- paste ("Year effects & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add5 <- paste ("State effects & \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add6 <- paste ("num. obs. & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs07[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs07[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs07[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs07[4],0)), "}  \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 10
addtorow$pos[[5]] <- 10
addtorow$pos[[6]] <- 20
addtorow$pos[[7]] <- 20
addtorow$pos[[8]] <- 20
addtorow$pos[[9]] <- 20
addtorow$pos[[10]] <- 20
addtorow$pos[[11]] <- 20
addtorow$command <- c (Header1, Header3, Header4
                       , add1a, Header5
                       , add2a, add2b, add3, add4, add5, add6)
print (xtable (  cbind (rows, parSE (tab.results07, n.digits=n.digit))
                 , align=c("l","l","c","c","c","c","c")
                 , digits=n.digit
                 , caption="2SLS estimates: effect of \\emph{proportion of copartisan mayors} on a congressional candidate's \\emph{probability of victory} in Mexico, 2000-2012 (2.5pp margin, PRI only)"
                 , label="T:ivResults025PRI")
       , sanitize.rownames.function=function(x){x}
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="small"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow  )



## Code for replicating Table A7
(tab.coefs08 <- cbind (
  c (coef.gov.win12[c(2:4,1),1], coef.gov.win12pri[c (2:4,1),1])
  , c (coef.gov.win13[c(2:4,1),1], coef.gov.win13pri[c (2:4,1),1])
  , rep (NA, 8)
  , c (coef.rup.win12[c(2:4,1),1], coef.rup.win12pri[c (2:4,1),1])
  , c (coef.rup.win13[c(2:4,1),1], coef.rup.win13pri[c (2:4,1),1])
))
(tab.ses08 <- cbind (
  c (coef.gov.win12[c(2:4,1),2], coef.gov.win12pri[c (2:4,1),2])
  , c (coef.gov.win13[c(2:4,1),2], coef.gov.win13pri[c (2:4,1),2])
  , rep (NA, 8)
  , c (coef.rup.win12[c(2:4,1),2], coef.rup.win12pri[c (2:4,1),2])
  , c (coef.rup.win13[c(2:4,1),2], coef.rup.win13pri[c (2:4,1),2])
))

odd <- seq (1, nrow (tab.coefs08)*2, by=2)
even <- seq (2, nrow (tab.coefs08)*2, by=2)

(tab.results08 <- data.frame (rbind (tab.coefs08, tab.ses08)))
rownames (tab.results08) <- c (odd, even)
tab.results08 <- tab.results08[order (as.numeric (rownames (tab.results08))),]

rows <- c ("Proportion of copartisan mayors", ""
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Intercept", ""
           , "Proportion of copartisan mayors", ""
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Intercept", "")

(nobs08 <- c (
  gov.win12$df.null, gov.win13$df.null
  , rup.win12$df.null, rup.win13$df.null) + 1)
(nobs08pri <- c (
  gov.win12pri$df.null, gov.win13pri$df.null
  , rup.win12pri$df.null, rup.win13pri$df.null) + 1)
(ndev08 <- c (
  gov.win12$null.deviance, gov.win13$null.deviance
  , rup.win12$null.deviance, rup.win13$null.deviance))
(ndev08pri <- c (
  gov.win12pri$null.deviance, gov.win13pri$null.deviance
  , rup.win12pri$null.deviance, rup.win13pri$null.deviance))
(dev08 <- c (
  gov.win12$deviance, gov.win13$deviance
  , rup.win12$deviance, rup.win13$deviance))
(dev08pri <- c (
  gov.win12pri$deviance, gov.win13pri$deviance
  , rup.win12pri$deviance, rup.win13pri$deviance))
(aic08 <- c (
  gov.win12$aic, gov.win13$aic
  , rup.win12$aic, rup.win13$aic))
(aic08pri <- c (
  gov.win12pri$aic, gov.win13pri$aic
  , rup.win12pri$aic, rup.win13pri$aic))

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\emph{Copartisan governor}} && \\multicolumn{2}{c}{\\emph{Opposition governor}} \\\\ \\cline{2-3} \\cline{5-6} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} && \\multicolumn{1}{c}{(3)} & \\multicolumn{1}{c}{(4)} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{6}{l}{\\underline{(a) Full Sample}} \\\\ [1.5ex] \n")
Header5 <- paste ("\\multicolumn{6}{l}{\\underline{(b) PRI-only Sample}} \\\\ [1.5ex] \n")

add1a <- paste ("\\midrule Null deviance & \\multicolumn{1}{c}{", sprintf ("%.1f", round (ndev08[1],1)), "} & \\multicolumn{1}{c}{", sprintf ("%.1f", round (ndev08[2],1)), "} && \\multicolumn{1}{c}{", sprintf ("%.1f", round (ndev08[3],1)), "} & \\multicolumn{1}{c}{", sprintf ("%.1f", round (ndev08[4],1)), "}  \\\\ \n")
add1b <- paste ("Residual deviance & \\multicolumn{1}{c}{", sprintf ("%.1f", round (dev08[1],1)), "} & \\multicolumn{1}{c}{", sprintf ("%.1f", round (dev08[2],1)), "} && \\multicolumn{1}{c}{", sprintf ("%.1f", round (dev08[3],1)), "} & \\multicolumn{1}{c}{", sprintf ("%.1f", round (dev08[4],1)), "}  \\\\ \n")
add1c <- paste ("AIC & \\multicolumn{1}{c}{", sprintf ("%.1f", round (aic08[1],1)), "} & \\multicolumn{1}{c}{", sprintf ("%.1f", round (aic08[2],1)), "} && \\multicolumn{1}{c}{", sprintf ("%.1f", round (aic08[3],1)), "} & \\multicolumn{1}{c}{", sprintf ("%.1f", round (aic08[4],1)), "}  \\\\ \n")
add1d <- paste ("num. obs. & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs08[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs08[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs08[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs08[4],0)), "}  \\\\ \\midrule \n")
add2a <- paste ("\\midrule Null deviance & \\multicolumn{1}{c}{", sprintf ("%.1f", round (ndev08pri[1],1)), "} & \\multicolumn{1}{c}{", sprintf ("%.1f", round (ndev08pri[2],1)), "} && \\multicolumn{1}{c}{", sprintf ("%.1f", round (ndev08pri[3],1)), "} & \\multicolumn{1}{c}{", sprintf ("%.1f", round (ndev08pri[4],1)), "}  \\\\ \n")
add2b <- paste ("Residual deviance & \\multicolumn{1}{c}{", sprintf ("%.1f", round (dev08pri[1],1)), "} & \\multicolumn{1}{c}{", sprintf ("%.1f", round (dev08pri[2],1)), "} && \\multicolumn{1}{c}{", sprintf ("%.1f", round (dev08pri[3],1)), "} & \\multicolumn{1}{c}{", sprintf ("%.1f", round (dev08pri[4],1)), "}  \\\\ \n")
add2c <- paste ("AIC & \\multicolumn{1}{c}{", sprintf ("%.1f", round (aic08pri[1],1)), "} & \\multicolumn{1}{c}{", sprintf ("%.1f", round (aic08pri[2],1)), "} && \\multicolumn{1}{c}{", sprintf ("%.1f", round (aic08pri[3],1)), "} & \\multicolumn{1}{c}{", sprintf ("%.1f", round (aic08pri[4],1)), "}  \\\\ \n")
add2d <- paste ("num. obs. & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs08pri[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs08pri[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs08pri[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs08pri[4],0)), "}  \\\\ \\midrule \n")
add3 <- paste ("Previous vote share & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add4 <- paste ("Year effects & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add5 <- paste ("State effects & \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 8
addtorow$pos[[5]] <- 8
addtorow$pos[[6]] <- 8
addtorow$pos[[7]] <- 8
addtorow$pos[[8]] <- 8
addtorow$pos[[9]] <- 16
addtorow$pos[[10]] <- 16
addtorow$pos[[11]] <- 16
addtorow$pos[[12]] <- 16
addtorow$pos[[13]] <- 16
addtorow$pos[[14]] <- 16
addtorow$pos[[15]] <- 16
addtorow$command <- c (Header1, Header3, Header4
                       , add1a, add1b, add1c, add1d, Header5
                       , add2a, add2b, add2c, add2d, add3, add4, add5)
print (xtable (  cbind (rows, parSE (tab.results08, n.digits=n.digit))
                 , align=c("l","l","c","c","c","c","c")
                 , digits=n.digit
                 , caption="Probit estimates: \\emph{proportion of copartisan mayors} on a congressional candidate's \\emph{probability of victory} in Mexico, 2000-2012"
                 , label="T:robustProbit")
       , sanitize.rownames.function=function(x){x}
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="small"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow  )



## Code for replicating Table A8
(tab.coefs09 <- cbind (
  c (coef(sum.gov.sh02)[c(2:4,1),1], coef(sum.gov.sh02pri)[c (2:4,1),1])
  , c (coef(sum.gov.sh03)[c(2:4,1),1], coef(sum.gov.sh03pri)[c (2:4,1),1])
  , rep (NA, 8)
  , c (coef(sum.rup.sh02)[c(2:4,1),1], coef(sum.rup.sh02pri)[c (2:4,1),1])
  , c (coef(sum.rup.sh03)[c(2:4,1),1], coef(sum.rup.sh03pri)[c (2:4,1),1])
))
(tab.ses09 <- cbind (
  c (coef(sum.gov.sh02)[c(2:4,1),2], coef(sum.gov.sh02pri)[c (2:4,1),2])
  , c (coef(sum.gov.sh03)[c(2:4,1),2], coef(sum.gov.sh03pri)[c (2:4,1),2])
  , rep (NA, 8)
  , c (coef(sum.rup.sh02)[c(2:4,1),2], coef(sum.rup.sh02pri)[c (2:4,1),2])
  , c (coef(sum.rup.sh03)[c(2:4,1),2], coef(sum.rup.sh03pri)[c (2:4,1),2])
))

odd <- seq (1, nrow (tab.coefs09)*2, by=2)
even <- seq (2, nrow (tab.coefs09)*2, by=2)

(tab.results09 <- data.frame (rbind (tab.coefs09, tab.ses09)))
rownames (tab.results09) <- c (odd, even)
tab.results09 <- tab.results09[order (as.numeric (rownames (tab.results09))),]

rows <- c ("Proportion of copartisan mayors", ""
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Intercept", ""
           , "Proportion of copartisan mayors", ""
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Intercept", "")

(nobs09 <- c (
  sum.gov.sh02$N, sum.gov.sh03$N
  , sum.rup.sh02$N, sum.rup.sh03$N))
(nobs09pri <- c (
  sum.gov.sh02pri$N, sum.gov.sh03pri$N
  , sum.rup.sh02pri$N, sum.rup.sh03pri$N))
(sigma03 <- c (
  sum.gov.sh02$rse, sum.gov.sh03$rse
  , sum.rup.sh02$rse, sum.rup.sh03$rse))
(sigma03pri <- c (
  sum.gov.sh02pri$rse, sum.gov.sh03pri$rse
  , sum.rup.sh02pri$rse, sum.rup.sh03pri$rse))

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\emph{Copartisan governor}} && \\multicolumn{2}{c}{\\emph{Opposition governor}} \\\\ \\cline{2-3} \\cline{5-6} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} && \\multicolumn{1}{c}{(3)} & \\multicolumn{1}{c}{(4)} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{6}{l}{\\underline{(a) Full Sample}} \\\\ [1.5ex] \n")
Header5 <- paste ("\\multicolumn{6}{l}{\\underline{(b) PRI-only Sample}} \\\\ [1.5ex] \n")

add1a <- paste ("\\midrule RMSE & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03[1],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03[2],2)), "} && \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03[3],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03[4],2)), "}  \\\\ \n")
add1b <- paste ("num. obs. & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs09[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs09[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs09[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs09[4],0)), "}  \\\\ \\midrule \n")
add2a <- paste ("\\midrule RMSE & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03pri[1],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03pri[2],2)), "} && \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03pri[3],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma03pri[4],2)), "}  \\\\ \n")
add2b <- paste ("num. obs. & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs09pri[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs09pri[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs09pri[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs09pri[4],0)), "}  \\\\ \\midrule \n")
add3 <- paste ("Previous vote share & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add4 <- paste ("Year effects & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add5 <- paste ("State effects & \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 8
addtorow$pos[[5]] <- 8
addtorow$pos[[6]] <- 8
addtorow$pos[[7]] <- 16
addtorow$pos[[8]] <- 16
addtorow$pos[[9]] <- 16
addtorow$pos[[10]] <- 16
addtorow$pos[[11]] <- 16
addtorow$command <- c (Header1, Header3, Header4
                       , add1a, add1b, Header5
                       , add2a, add2b, add3, add4, add5)
print (xtable (  cbind (rows, parSE (tab.results09, n.digits=n.digit))
                 , align=c("l","l","c","c","c","c","c")
                 , digits=n.digit
                 , caption="OLS estimates: \\emph{proportion of copartisan mayors} on a congressional candidate's \\emph{vote share}"
                 , label="T:robustShare")
       , sanitize.rownames.function=function(x){x}
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="small"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow  )



## Code for replicating Table A9
(tab.coefs10 <- cbind (
  c (coef(sum.gov.mg02)[c(2:4,1),1], coef(sum.gov.mg02pri)[c (2:4,1),1])
  , c (coef(sum.gov.mg03)[c(2:4,1),1], coef(sum.gov.mg03pri)[c (2:4,1),1])
  , rep (NA, 8)
  , c (coef(sum.rup.mg02)[c(2:4,1),1], coef(sum.rup.mg02pri)[c (2:4,1),1])
  , c (coef(sum.rup.mg03)[c(2:4,1),1], coef(sum.rup.mg03pri)[c (2:4,1),1])
))
(tab.ses10 <- cbind (
  c (coef(sum.gov.mg02)[c(2:4,1),2], coef(sum.gov.mg02pri)[c (2:4,1),2])
  , c (coef(sum.gov.mg03)[c(2:4,1),2], coef(sum.gov.mg03pri)[c (2:4,1),2])
  , rep (NA, 8)
  , c (coef(sum.rup.mg02)[c(2:4,1),2], coef(sum.rup.mg02pri)[c (2:4,1),2])
  , c (coef(sum.rup.mg03)[c(2:4,1),2], coef(sum.rup.mg03pri)[c (2:4,1),2])
  ))

odd <- seq (1, nrow (tab.coefs10)*2, by=2)
even <- seq (2, nrow (tab.coefs10)*2, by=2)

(tab.results10 <- data.frame (rbind (tab.coefs10, tab.ses10)))
rownames (tab.results10) <- c (odd, even)
tab.results10 <- tab.results10[order (as.numeric (rownames (tab.results10))),]

rows <- c ("Proportion of copartisan mayors", ""
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Intercept", ""
           , "Proportion of copartisan mayors", ""
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Intercept", "")

(nobs10 <- c (
  sum.gov.mg02$N, sum.gov.mg03$N
  , sum.rup.mg02$N, sum.rup.mg03$N))
(nobs10pri <- c (
  sum.gov.mg02pri$N, sum.gov.mg03pri$N
  , sum.rup.mg02pri$N, sum.rup.mg03pri$N))
(sigma10 <- c (
  sum.gov.mg02$rse, sum.gov.mg03$rse
  , sum.rup.mg02$rse, sum.rup.mg03$rse))
(sigma10pri <- c (
  sum.gov.mg02pri$rse, sum.gov.mg03pri$rse
  , sum.rup.mg02pri$rse, sum.rup.mg03pri$rse))

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\emph{Copartisan governor}} && \\multicolumn{2}{c}{\\emph{Opposition governor}} \\\\ \\cline{2-3} \\cline{5-6} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} && \\multicolumn{1}{c}{(3)} & \\multicolumn{1}{c}{(4)} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{6}{l}{\\underline{(a) Full Sample}} \\\\ [1.5ex] \n")
Header5 <- paste ("\\multicolumn{6}{l}{\\underline{(b) PRI-only Sample}} \\\\ [1.5ex] \n")

add1a <- paste ("\\midrule RMSE & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma10[1],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma10[2],2)), "} && \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma10[3],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma10[4],2)), "}  \\\\ \n")
add1b <- paste ("num. obs. & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs10[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs10[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs10[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs10[4],0)), "}  \\\\ \\midrule \n")
add2a <- paste ("\\midrule RMSE & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma10pri[1],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma10pri[2],2)), "} && \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma10pri[3],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma10pri[4],2)), "}  \\\\ \n")
add2b <- paste ("num. obs. & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs10pri[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs10pri[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs10pri[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs10pri[4],0)), "}  \\\\ \\midrule \n")
add3 <- paste ("Previous vote share & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add4 <- paste ("Year effects & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add5 <- paste ("State effects & \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 8
addtorow$pos[[5]] <- 8
addtorow$pos[[6]] <- 8
addtorow$pos[[7]] <- 16
addtorow$pos[[8]] <- 16
addtorow$pos[[9]] <- 16
addtorow$pos[[10]] <- 16
addtorow$pos[[11]] <- 16
addtorow$command <- c (Header1, Header3, Header4
                       , add1a, add1b, Header5
                       , add2a, add2b, add3, add4, add5)
print (xtable (  cbind (rows, parSE (tab.results10, n.digits=n.digit))
                 , align=c("l","l","c","c","c","c","c")
                 , digits=n.digit
                 , caption="OLS estimates: \\emph{proportion of copartisan mayors} on a congressional candidate's \\emph{margin of victory}"
                 , label="T:robustMargin")
       , sanitize.rownames.function=function(x){x}
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="small"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow  )



## Code for replicating Table A10
(tab.coefs11 <- cbind (
  c (coef(sum.gov.win05)[c(2:5,1),1], coef(sum.gov.win05pri)[c (2:5,1),1])
  , c (coef(sum.gov.win06)[c(2:5,1),1], coef(sum.gov.win06pri)[c (2:5,1),1])
  , rep (NA, 10)
  , c (coef(sum.rup.win05)[c(2:5,1),1], coef(sum.rup.win05pri)[c (2:5,1),1])
  , c (coef(sum.rup.win06)[c(2:5,1),1], coef(sum.rup.win06pri)[c (2:5,1),1])
))
(tab.ses11 <- cbind (
  c (coef(sum.gov.win05)[c(2:5,1),2], coef(sum.gov.win05pri)[c (2:5,1),2])
  , c (coef(sum.gov.win06)[c(2:5,1),2], coef(sum.gov.win06pri)[c (2:5,1),2])
  , rep (NA, 10)
  , c (coef(sum.rup.win05)[c(2:5,1),2], coef(sum.rup.win05pri)[c (2:5,1),2])
  , c (coef(sum.rup.win06)[c(2:5,1),2], coef(sum.rup.win06pri)[c (2:5,1),2])
  ))

odd <- seq (1, nrow (tab.coefs11)*2, by=2)
even <- seq (2, nrow (tab.coefs11)*2, by=2)

(tab.results11 <- data.frame (rbind (tab.coefs11, tab.ses11)))
rownames (tab.results11) <- c (odd, even)
tab.results11 <- tab.results11[order (as.numeric (rownames (tab.results11))),]

rows <- c ("Proportion of copartisan mayors", ""
           , "Effective number of municipalities", ""
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Intercept", ""
           , "Proportion of copartisan mayors", ""
           , "Effective number of municipalities", ""
           , "Margin of victory (lagged)", ""
           , "Poverty", ""
           , "Intercept", "")

(nobs11 <- c (
  sum.gov.win05$N, sum.gov.win06$N
  , sum.rup.win05$N, sum.rup.win06$N))
(nobs11pri <- c (
  sum.gov.win05pri$N, sum.gov.win06pri$N
  , sum.rup.win05pri$N, sum.rup.win06pri$N))
(sigma11 <- c (
  sum.gov.win05$rse, sum.gov.win06$rse
  , sum.rup.win05$rse, sum.rup.win06$rse))
(sigma11pri <- c (
  sum.gov.win05pri$rse, sum.gov.win06pri$rse
  , sum.rup.win05pri$rse, sum.rup.win06pri$rse))

Header1 <- paste ("\\toprule & \\multicolumn{2}{c}{\\emph{Copartisan governor}} && \\multicolumn{2}{c}{\\emph{Opposition governor}} \\\\ \\cline{2-3} \\cline{5-6} \n")
Header3 <- paste (" & \\multicolumn{1}{c}{(1)} & \\multicolumn{1}{c}{(2)} && \\multicolumn{1}{c}{(3)} & \\multicolumn{1}{c}{(4)} \\\\ \\midrule \n")
Header4 <- paste ("\\multicolumn{6}{l}{\\underline{(a) Full Sample}} \\\\ [1.5ex] \n")
Header5 <- paste ("\\multicolumn{6}{l}{\\underline{(b) PRI-only Sample}} \\\\ [1.5ex] \n")

add1a <- paste ("\\midrule RMSE & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma11[1],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma11[2],2)), "} && \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma11[3],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma11[4],2)), "}  \\\\ \n")
add1b <- paste ("num. obs. & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs11[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs11[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs11[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs11[4],0)), "}  \\\\ \\midrule \n")
add2a <- paste ("\\midrule RMSE & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma11pri[1],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma11pri[2],2)), "} && \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma11pri[3],2)), "} & \\multicolumn{1}{c}{", sprintf ("%.2f", round (sigma11pri[4],2)), "}  \\\\ \n")
add2b <- paste ("num. obs. & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs11pri[1],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs11pri[2],0)), "} && \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs11pri[3],0)), "} & \\multicolumn{1}{c}{", sprintf ("%.0f", round (nobs11pri[4],0)), "}  \\\\ \\midrule \n")
add3 <- paste ("Previous vote share & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add4 <- paste ("Year effects & \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{Yes} & \\multicolumn{1}{c}{Yes} \\\\ \n")
add5 <- paste ("State effects & \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} && \\multicolumn{1}{c}{No} & \\multicolumn{1}{c}{Yes} \\\\ \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 0
addtorow$pos[[4]] <- 10
addtorow$pos[[5]] <- 10
addtorow$pos[[6]] <- 10
addtorow$pos[[7]] <- 20
addtorow$pos[[8]] <- 20
addtorow$pos[[9]] <- 20
addtorow$pos[[10]] <- 20
addtorow$pos[[11]] <- 20
addtorow$command <- c (Header1, Header3, Header4
                       , add1a, add1b, Header5
                       , add2a, add2b, add3, add4, add5)
print (xtable (  cbind (rows, parSE (tab.results11, n.digits=n.digit))
                 , align=c("l","l","c","c","c","c","c")
                 , digits=n.digit
                 , caption="OLS estimates: controlling for the effective number of municipalities"
                 , label="T:robustENM")
       , sanitize.rownames.function=function(x){x}
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="small"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow  )



## Code for replicating Table A11
(coefWin <- rbind (
  with (gov.win01rdd, c (coef[2], se[2], pv[2], bws[1], N_h_l, N_h_r))
  , with (rup.win01rdd, c (coef[2], se[2], pv[2], bws[1], N_h_l, N_h_r))
  , with (gov.tnt01rdd, c (coef[2], se[2], pv[2], bws[1], N_h_l, N_h_r))
  , with (rup.tnt01rdd, c (coef[2], se[2], pv[2], bws[1], N_h_l, N_h_r))
  ))

# adding the asterisks
(coefWin2 <- cbind (matrix (sprintf ("%.2f", round (coefWin[,c (1:2,4)], 3)), ncol=3)
                    , matrix (sprintf ("%.0f", coefWin[,5:6]), ncol=2)))
coefWin2[,2] <- ifelse (coefWin[,3] <= 0.05, paste (coefWin2[,2], "$^{*}$", sep=""), ifelse (coefWin[,3] <= 0.10, paste (coefWin2[,2], "$^{\\dagger}$", sep=""), coefWin2[,2]))

rows <- rep (c ("Copartisan governor", "Non-copartisan governor"))

Header2 <- paste ("\\toprule \\multicolumn{1}{c}{} & \\multicolumn{1}{c}{LATE} & \\multicolumn{1}{c}{SE} & \\multicolumn{1}{c}{bwd.} & \\multicolumn{1}{c}{$N^{-}$} & \\multicolumn{1}{c}{$N^{+}$} \\\\ \\midrule \n")
Header3 <- paste ("\\multicolumn{6}{l}{\\underline{Local linear regression} (outcome: \\emph{copartisan victory})} \\\\ [1ex] \n")
Header4 <- paste ("[2.5ex] \\multicolumn{6}{l}{\\underline{Local linear regression} (outcome: \\emph{turnout})} \\\\ [1ex] \n")

addtorow <- list ()
addtorow$pos <- list ()
addtorow$pos[[1]] <- 0
addtorow$pos[[2]] <- 0
addtorow$pos[[3]] <- 2
addtorow$command <- c (Header2, Header3, Header4)
print (xtable (cbind (rows, coefWin2)
               , align=c("l","l","r","l","c","r","c")
               , digits=n.digit
               , caption="RD estimates of \\emph{municipal incumbency} on the municipal-level performance of Mexican congressional candiates, 2000-2012"
               , label="T:rdResults")
       , sanitize.rownames.function=function(x){x}
       , sanitize.text.function=function(x){x}
       , floating=TRUE
       , table.placement="t"
       , caption.placement="top" 
       , latex.environments="center"
       , size="small"
       , include.colnames=FALSE
       , include.rownames=FALSE
       , hline.after = c ()
       , add.to.row=addtorow)

sink (NULL)